<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <input type="text" id="txt">
</body>
<script>
  // debounce的作用是在让在用户动作停止后延迟x ms再执行回调。  多次触发事件后，事件处理函数只执行一次，并且是在触发操作结束时执行。
  let $text = document.getElementById('txt')
  $text.oninput = debounce(search, 2000)


  function search() {
    console.log(this.value)
  }

  function debounce(method, delay) {
    let timer = null;
    return function () {
      let self = this,
        args = arguments;
      timer && clearTimeout(timer);
      timer = setTimeout(function () {
        method.apply(self, args);
      }, delay);
    }
  }
</script>

</html>